package com.example.androidcomplete0102.ch19;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.example.androidcomplete0102.R;

public class ThreadEx extends Activity {
	int mainValue;
	int backValue;
	TextView mainTv;
	TextView backTv;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch19_threadex);
		
		mainTv = (TextView)findViewById(R.id.ch19_mainvalue);
		backTv = (TextView)findViewById(R.id.ch19_backvalue);
		
		BackThread thread = new BackThread();
		thread.setDaemon(true);//threadEx 액티비티가 종료되면 BackThread도 종료되도록 설정
		thread.start();
	}
	
	public void mOnClick(View v){
		if(v.getId() == R.id.ch19_increase){
			mainValue++;
			mainTv.setText("MainValue:"+mainValue);
//			backTv.setText("backValue:"+backValue);
		}
	}
	
	class BackThread extends Thread{
		@Override
		public void run() {
			while(true){
				backValue++;
				//backTv.setText("backValue:"+backValue);이거 안됨
				try {
					sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
